﻿using Launchkit_Backend.model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Launchkit_Backend
{
    public partial class CtlTile : UserControl
    {
        public const int CellSize = 48;
        private const int CellInner = 2;

        public const int WrapperWidth = 15;
        public const int WrapperHeight = 10;


        public Entry bindEntry;

        public bool Changed = false;
        private int _x, _y, _w, _h;


        private string _title="";


        public string Title
        {
            get {
                return _title;
            }

            set {
                _title = value;
                W = W;
                if (bindEntry != null)
                {
                    bindEntry.DisplayName= value;
                    changed = true;
                }
            }
        }


        private bool isSmallIcon = false;
        private bool changed = false;
        private Image _icon = Properties.Resources.hotdata_small__自定义_;
        public Image Icon { get => _icon;
            set {
                changed = true;
                _icon = value;
                updateIcon();
            } 
        }

        private string _iconName = "";
        public string IconName
        {
            get {
                return _iconName;
            }

            set {
                _iconName = value;
                if (bindEntry != null)
                {
                    bindEntry.DisplayIcon = value;
                    changed = true;
                }
                if (!DesignMode) {
                    Icon = Program.IconProvider.GetIcon(value);
                }
            }
        }

        public void bindToEntry(Entry e) {
            this.bindEntry = null;
            this.X = e.EntryData.x;
            this.Y = e.EntryData.y;
            this.W = e.EntryData.w;
            this.H = e.EntryData.h;
            this.IconName = e.DisplayIcon;
            this.Title = e.DisplayName;
            this.bindEntry = e;
            
        }

        public void saveEntry() {
            this.bindEntry.Save();
            Changed = false;
        }

        

        private void updateIcon() {
            bool shouldSmallIcon = W < 2 || H < 2;
            if (changed) {
                changed = false;
                isSmallIcon = !shouldSmallIcon;
            }
            
            if (shouldSmallIcon != isSmallIcon) {
                if (shouldSmallIcon)
                {
                     button1.Image = new Bitmap(Icon, 16, 16);
                }
                else {
                     button1.Image = Icon;
                }
                isSmallIcon = shouldSmallIcon;
            }

        }


        public int X { get => _x; 
            set { 
                _x = value;
                this.Left = _x * CellSize + CellInner;
                if (bindEntry != null) {
                    bindEntry.EntryData.x = _x;
                    changed = true;
                }
            } 
        }
        public int Y
        {
            get => _y;
            set
            {
                _y = value; 
                this.Top = _y * CellSize + CellInner; 
                if (bindEntry != null)
                {
                    bindEntry.EntryData.y = _y;
                    changed = true;
                }
            }
        }

        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            OnMouseDown(e);
        }

        private void button1_MouseUp(object sender, MouseEventArgs e)
        {
            OnMouseUp(e);
        }

        private void button1_MouseMove(object sender, MouseEventArgs e)
        {
            OnMouseMove(e);
        }

        public int W
        {
            get => _w;
            set
            {
                
                _w = value;
                if (_w >= 2 && _h >=2)
                {
                    button1.Text = Title;
                    toolTip1.RemoveAll();
                }
                else {
                    button1.Text = "";
                    toolTip1.SetToolTip(button1, Title);
                }
                this.Width = _w * CellSize - CellInner * 2;
                if (bindEntry != null)
                {
                    bindEntry.EntryData.w = _w;
                    changed = true;
                }
                updateIcon();
            }
        }
        public int H
        {
            get => _h;
            set
            {
                _h = value;
                this.Height = _h * CellSize - CellInner * 2;
                if (_w >= 2 && _h >= 2)
                {
                    button1.Text = Title;
                    toolTip1.RemoveAll();
                }
                else
                {
                    button1.Text = "";
                    toolTip1.SetToolTip(button1, Title);
                }
                if (bindEntry != null)
                {
                    bindEntry.EntryData.h = _h;
                    changed = true;
                }
                updateIcon();
            }
        }

       
        public CtlTile()
        {
            InitializeComponent();
        }

        internal void PerformAction(Form1 form1)
        {
            if (bindEntry.Type == Entry.TypeCollection) {
                bindEntry.EntryData.param = bindEntry.ID.ToString();
                bindEntry.EntryData.action = EntryActions.ActionLink;
            }
            if (EntryActions.actions.ContainsKey(bindEntry.EntryData.action)) {
                EntryActions.actions[bindEntry.EntryData.action](form1, bindEntry.EntryData.param);
            }
            DBUtils.recordHistory(bindEntry);
        }
    }
}
